//package com.zjjh;
//
//import org.springframework.cloud.gateway.config.GatewayProperties;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
//import org.springframework.cloud.gateway.support.NotFoundException;
//import org.springframework.stereotype.Component;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.util.*;
//
//@Component
//public class DBRouteDefinitionRepository implements RouteDefinitionRepository {
//
//    private final Map<String, RouteDefinition> routes = Collections.synchronizedMap(new LinkedHashMap());
//
//    //初始標準
//    private boolean init_flag = true;
//
//    private final GatewayProperties properties;
//
//    private DynamicRouteServiceImpl service;
//
//    public DBRouteDefinitionRepository(GatewayProperties properties) {
//        this.properties = properties;
//        this.service = new DynamicRouteServiceImpl();
//    }
//
//    @Override
//    public Flux<RouteDefinition> getRouteDefinitions() {
//        if(init_flag) {
//            List<RouteDefinition> routeDefinitions = new ArrayList<>();
//            List<RouteDefinition> rs = new ArrayList<>();
//            try {
//                routeDefinitions = service.quertAllRoutes();//从数据库中加载route
//                rs = this.properties.getRoutes();//获得配置文件的route
//                for (RouteDefinition rse : rs) {
//                    routeDefinitions.add(rse);
//                }
//                routes.clear();
//                routeDefinitions.forEach(x->routes.put(x.getId(), x));
//                init_flag = false;
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return Flux.fromIterable(routeDefinitions);
//        }else {
//            return Flux.fromIterable(routes.values());
//        }
//
//    }
//
//    @Override
//    public Mono<Void> delete(Mono<String> routeId) {
//        return routeId.flatMap(id -> {
//            if (routes.containsKey(id)) {
//                routes.remove(id);
//                return Mono.empty();
//            }
//            return Mono.defer(() -> Mono.error(new NotFoundException("RouteDefinition not found: " + routeId)));
//        });
//    }
//
//    @Override
//    public Mono<Void> save(Mono<RouteDefinition> route) {
//        return route.flatMap( r -> {
//            routes.put(r.getId(), r);
//            return Mono.empty();
//        });
//    }
//}